package com.miracle.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.miracle.common.R;


public class ToastCommonUtils {
    @ColorInt
    private static int SDEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF");
    @ColorInt
    private static int SERROR_COLOR = Color.parseColor("#D50000");
    @ColorInt
    private static int SINFO_COLOR = Color.parseColor("#3F51B5");
    @ColorInt
    private static int SSUCCESS_COLOR = Color.parseColor("#388E3C");
    @ColorInt
    private static int SWARNING_COLOR = Color.parseColor("#FFA900");
    @ColorInt
    private static int SNORMAL_COLOR = Color.parseColor("#353A3E");
    /**
     * 设置字体
     */
    private static final Typeface LOADED_TOAST_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
    private static Typeface currentTypeface = LOADED_TOAST_TYPEFACE;
    private static int textSize = 16;

    private static boolean tintIcon = true;

    private ToastCommonUtils() {
        // avoiding instantiation
    }
    private static  Toast mToast = null;//全局唯一的Toast



    /**
     * 普通文本提示（没有左边图标）
     *
     * @param context
     * @param message 提示文本
     * @return
     */
    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message) {

        return normal(context, message, Toast.LENGTH_SHORT, null, false);
    }

    public static Toast normal(@NonNull Context context, @StringRes int id) {
        return normal(context, context.getString(id), Toast.LENGTH_SHORT, null, false);
    }

    /**
     * 普通提示
     *
     * @param context
     * @param message 提示文本
     * @param icon    左边图标
     * @return
     */

    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message, Drawable icon) {
        return normal(context, message, Toast.LENGTH_SHORT, icon, true);
    }

    /**
     * 普通提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @return
     */
    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration) {
        return normal(context, message, duration, null, false);
    }

    /**
     * 普通提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @param icon     左边图标
     * @return
     */
    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration,
                               Drawable icon) {
        return normal(context, message, duration, icon, true);
    }

    /**
     * 普通提示
     *
     * @param context
     * @param message    提示文本
     * @param duration   弹出显示时长 short or long
     * @param icon       左边图标
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration,
                               Drawable icon, boolean isLeftIcon) {
        return custom(context, message, icon, SNORMAL_COLOR, duration, isLeftIcon, true);
    }

    /**
     * 警告提示
     *
     * @param context
     * @param message 提示文本
     * @return
     */
    @CheckResult
    public static Toast warning(@NonNull Context context, @NonNull CharSequence message) {
        return warning(context, message, Toast.LENGTH_SHORT, true);
    }

    /**
     * 警告提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @return
     */
    @CheckResult
    public static Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration) {
        return warning(context, message, duration, true);
    }

    /**
     * 警告提示
     *
     * @param context
     * @param message    提示文本
     * @param duration   弹出显示时长 short or long
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration, boolean isLeftIcon) {
        return custom(context, message, ToastHelpUtils.getDrawable(context, R.mipmap.ic_launcher_round),
                SWARNING_COLOR, duration, isLeftIcon, true);
    }

    /**
     * 信息提示
     *
     * @param context
     * @param message 提示文本
     * @return
     */
    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message) {
        return info(context, message, Toast.LENGTH_SHORT, true);
    }

    /**
     * 信息提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @return
     */
    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message, int duration) {
        return info(context, message, duration, true);
    }

    /**
     * 信息提示
     *
     * @param context
     * @param message    提示文本
     * @param duration   弹出显示时长 short or long
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast info(@NonNull Context context, @NonNull CharSequence message, int duration, boolean isLeftIcon) {
        return custom(context, message, ToastHelpUtils.getDrawable(context, R.mipmap.ic_launcher_round),
                SINFO_COLOR, duration, isLeftIcon, true);
    }

    /**
     * 成功提示
     *
     * @param context
     * @param message 提示文本
     * @return
     */
    @CheckResult
    public static Toast success(@NonNull Context context, @NonNull CharSequence message) {
        return success(context, message, Toast.LENGTH_SHORT, true);
    }

    /**
     * 成功提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @return
     */
    @CheckResult
    public static Toast success(@NonNull Context context, @NonNull CharSequence message, int duration) {
        return success(context, message, duration, true);
    }

    /**
     * 成功提示
     *
     * @param context
     * @param message    提示文本
     * @param duration   弹出显示时长 short or long
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast success(@NonNull Context context, @NonNull CharSequence message, int duration, boolean isLeftIcon) {
        return custom(context, message, ToastHelpUtils.getDrawable(context, R.mipmap.ic_launcher_round),
                SSUCCESS_COLOR, duration, isLeftIcon, true);
    }

    /**
     * 错误提示
     *
     * @param context
     * @param message 提示文本
     * @return
     */
    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message) {
        return error(context, message, Toast.LENGTH_SHORT, true);
    }

    /**
     * 错误提示
     *
     * @param context
     * @param message  提示文本
     * @param duration 弹出显示时长 short or long
     * @return
     */
    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message, int duration) {
        return error(context, message, duration, true);
    }

    /**
     * 错误提示
     *
     * @param context
     * @param message    提示文本
     * @param duration   弹出显示时长 short or long
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast error(@NonNull Context context, @NonNull CharSequence message, int duration, boolean isLeftIcon) {
        return custom(context, message, ToastHelpUtils.getDrawable(context, R.mipmap.ic_launcher_round),
                SERROR_COLOR, duration, isLeftIcon, true);
    }

    /**
     * 自定义提示（无背景颜色）
     *
     * @param context
     * @param message    提示文本
     * @param icon       左边小图标
     * @param duration   弹出显示时长 short or long
     * @param isLeftIcon 是否显示左边小图标
     * @return
     */
    @CheckResult
    public static Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
                               int duration, boolean isLeftIcon) {
        return custom(context, message, icon, -1, duration, isLeftIcon, false);
    }

    /**
     * 自定义提示 （可设置背景颜色）
     *
     * @param context
     * @param message      提示文本
     * @param iconRes      左边小图标
     * @param tintBgColor  背景颜色
     * @param duration     弹出显示时长 short or long
     * @param isLeftIcon   是否显示左边小图标
     * @param shouldBgTint 是否使用背景色
     * @return
     */
    @CheckResult
    public static Toast custom(@NonNull Context context, @NonNull CharSequence message, @DrawableRes int iconRes,
                               @ColorInt int tintBgColor, int duration,
                               boolean isLeftIcon, boolean shouldBgTint) {
        return custom(context, message, ToastHelpUtils.getDrawable(context, iconRes),
                tintBgColor, duration, isLeftIcon, shouldBgTint);
    }

    /**
     * 构建Toast
     *
     * @param context
     * @param message      文本提示语
     * @param icon         左边小图标
     * @param tintBgColor  背景颜色
     * @param duration     弹出显示时长 short or long
     * @param isLeftIcon   是否显示左边小图标
     * @param shouldBgTint 是否使用背景色
     * @return
     */
    @SuppressLint("ShowToast")
    @CheckResult
    public static Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
                               @ColorInt int tintBgColor, int duration,
                               boolean isLeftIcon, boolean shouldBgTint) {
        if (mToast == null) {
            mToast = new Toast(context);
        }
        final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.toast_layout, null);
        final ImageView toastIcon = toastLayout.findViewById(R.id.toast_icon);
        final TextView toastTextView = toastLayout.findViewById(R.id.toast_text);
        Drawable drawableFrame;
        /*
         *判断是否使用背景色
         */
        if (shouldBgTint) {
            drawableFrame = ToastHelpUtils.tint9PatchDrawableFrame(context, tintBgColor);
        } else {
            drawableFrame = ToastHelpUtils.getDrawable(context, R.drawable.toast_frame);
        }
        ToastHelpUtils.setBackground(toastLayout, drawableFrame);

        /*
         *判断是否显示左边小图标
         */
        if (isLeftIcon) {
            if (icon == null) {
                throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true");
            }
            if (tintIcon) {
                icon = ToastHelpUtils.tintIcon(icon, SDEFAULT_TEXT_COLOR);
            }
            ToastHelpUtils.setBackground(toastIcon, icon);
        } else {
            toastIcon.setVisibility(View.GONE);
        }

        //设置文本
        toastTextView.setText(message);
        //设置文本颜色
        toastTextView.setTextColor(SDEFAULT_TEXT_COLOR);
        //设置字体
        toastTextView.setTypeface(currentTypeface);
        //设置字体大小
        toastTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        //设置弹出显示时间
        mToast.setDuration(duration);
        mToast.setView(toastLayout);
        return mToast;
    }

    /**
     * 构建Toast
     *
     * @param context
     * @param message      文本提示语
     * @param icon         左边小图标
     * @param tintBgColor  背景颜色
     * @param duration     弹出显示时长 short or long
     * @param isLeftIcon   是否显示左边小图标
     * @param shouldBgTint 是否使用背景色
     * @param layout       Toast布局
     * @return
     */
    @SuppressLint("ShowToast")
    @CheckResult
    public static Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
                               @ColorInt int tintBgColor, int duration,
                               boolean isLeftIcon, boolean shouldBgTint, int layout) {
        if (mToast == null) {
            mToast = new Toast(context);
        }
        final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(layout, null);
        final ImageView toastIcon = toastLayout.findViewById(R.id.toast_icon);
        final TextView toastTextView = toastLayout.findViewById(R.id.toast_text);
        Drawable drawableFrame;
        /*
         *判断是否使用背景色
         */
        if (shouldBgTint) {
            drawableFrame = ToastHelpUtils.tint9PatchDrawableFrame(context, tintBgColor);
        } else {
            drawableFrame = ToastHelpUtils.getDrawable(context, R.drawable.toast_frame);
        }
        ToastHelpUtils.setBackground(toastLayout, drawableFrame);

        /*
         *判断是否显示左边小图标
         */
        if (isLeftIcon) {
            if (icon == null) {
                throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true");
            }
            if (tintIcon) {
                icon = ToastHelpUtils.tintIcon(icon, SDEFAULT_TEXT_COLOR);
            }
            ToastHelpUtils.setBackground(toastIcon, icon);
        } else {
            toastIcon.setVisibility(View.GONE);
        }

        //设置文本
        toastTextView.setText(message);
        //设置文本颜色
        toastTextView.setTextColor(SDEFAULT_TEXT_COLOR);
        //设置字体
        toastTextView.setTypeface(currentTypeface);
        //设置字体大小
        toastTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        //设置弹出显示时间
        mToast.setDuration(duration);
        mToast.setView(toastLayout);
        return mToast;
    }


    /**
     * 自定义配置
     */
    private static class Config {
        @ColorInt
        private int defaultTextColor = ToastCommonUtils.SDEFAULT_TEXT_COLOR;
        @ColorInt
        private int errorConfigColor = ToastCommonUtils.SERROR_COLOR;
        @ColorInt
        private int infoConfigColor = ToastCommonUtils.SINFO_COLOR;
        @ColorInt
        private int successConfigColor = ToastCommonUtils.SSUCCESS_COLOR;
        @ColorInt
        private int warningConfigColor = ToastCommonUtils.SWARNING_COLOR;

        private Typeface typeface = ToastCommonUtils.currentTypeface;
        private int textSize = ToastCommonUtils.textSize;

        private boolean tintIcon = ToastCommonUtils.tintIcon;

        private Config() {
            // avoiding instantiation
        }

        @CheckResult
        public static Config getInstance() {
            return new Config();
        }

        /**
         * 自定义配置后需要将toast重置
         * 否则其他Toast也会按照自定义配置显示
         * 所以需要将Toast重置为初始状态
         */
        public static void reset() {
            ToastCommonUtils.SDEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF");
            ToastCommonUtils.SERROR_COLOR = Color.parseColor("#D50000");
            ToastCommonUtils.SINFO_COLOR = Color.parseColor("#3F51B5");
            ToastCommonUtils.SSUCCESS_COLOR = Color.parseColor("#388E3C");
            ToastCommonUtils.SWARNING_COLOR = Color.parseColor("#FFA900");
            ToastCommonUtils.currentTypeface = LOADED_TOAST_TYPEFACE;
            ToastCommonUtils.textSize = 16;
            ToastCommonUtils.tintIcon = true;
        }

        @CheckResult
        public Config setTextColor(@ColorInt int textColor) {
            defaultTextColor = textColor;
            return this;
        }

        @CheckResult
        public Config setErrorColor(@ColorInt int errorColor) {
            errorConfigColor = errorColor;
            return this;
        }

        @CheckResult
        public Config setInfoColor(@ColorInt int infoColor) {
            infoConfigColor = infoColor;
            return this;
        }

        @CheckResult
        public Config setSuccessColor(@ColorInt int successColor) {
            successConfigColor = successColor;
            return this;
        }

        @CheckResult
        public Config setWarningColor(@ColorInt int warningColor) {
            warningConfigColor = warningColor;
            return this;
        }

        @CheckResult
        public Config setToastTypeface(@NonNull Typeface typeface) {
            this.typeface = typeface;
            return this;
        }

        @CheckResult
        public Config setTextSize(int sizeInSp) {
            this.textSize = sizeInSp;
            return this;
        }

        @CheckResult
        public Config tintIcon(boolean tintIcon) {
            this.tintIcon = tintIcon;
            return this;
        }

        /**
         * 自定义配置完需要调用此方法
         */
        public void apply() {
            ToastCommonUtils.SDEFAULT_TEXT_COLOR = defaultTextColor;
            ToastCommonUtils.SERROR_COLOR = errorConfigColor;
            ToastCommonUtils.SINFO_COLOR = infoConfigColor;
            ToastCommonUtils.SSUCCESS_COLOR = successConfigColor;
            ToastCommonUtils.SWARNING_COLOR = warningConfigColor;
            ToastCommonUtils.currentTypeface = typeface;
            ToastCommonUtils.textSize = textSize;
            ToastCommonUtils.tintIcon = tintIcon;
        }
    }

}
